package com.controller;

import com.pojo.Account;
import com.pojo.RightClassOne;
import com.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/loginContoller")
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * 1.获取登录参数
     * 2.获取用户是否勾选记住密码
     * 3.设置session 1.Account:存取用户登录信息, 2.RiOneList:存取用户菜单权限信息 3.RightDoList:存取用户操作权限信息
     * 4.校验 LoginReturnAccountMsgByUsernameAndPassword
     * 5.成功添加cookie，否则不添加
     */
    @RequestMapping("/login")
    public String login(String userName, String passWord, String checkboxNum, HttpServletRequest req, HttpServletResponse resp) throws IOException, ParseException {

        HttpSession session = req.getSession();
        Account account = new Account();
        account.setUserName(userName);
        account.setPassWord(passWord);
        Account thisAccount = loginService.LoginReturnAccountMsgByUsernameAndPassword(account);  //查询用户信息

        /**
         * 1.账号密码校验
         * 2.账号及角色禁用校验
         * 3.添加登录时间
         * 4.添加cookie
         */
        if (thisAccount != null) {

            if(thisAccount.getStatus()==1 && thisAccount.getRoleStatus()==1){

                thisAccount.setLoginTime(new Date());  //封装用户登录时间，刷新用户登录时间：loginService.addLoginTime(thisAccount)

                if (loginService.addLoginTime(thisAccount)) {
                    session.setAttribute("thisAccount", thisAccount);  //存用户信息
                    System.out.println("thisAccount:"+session.getAttribute("thisAccount"));

                    session.setAttribute("RiOneList", loginService.thisAccountAllRightClassById(thisAccount.getRoleId()));  //后台一级菜单权限
                    session.setAttribute("RecOneList", loginService.thisAccountRightFrontStageList(thisAccount.getRoleId())); //前台一级菜单权限
                    session.setAttribute("RightDoList", loginService.thisAccountRightDoByRoleId(thisAccount.getRoleId()));  //操作权限

                    //判断是否记住密码，是则使用cookie
                    if ("on".equals(checkboxNum)) {
                        System.out.println("用户选择了自动登录");
                        Cookie name = new Cookie("accountName", userName);
                        Cookie pwd = new Cookie("accountPwd", passWord);
                        name.setMaxAge(60 * 60 * 24);
                        pwd.setMaxAge(60 * 60 * 24);
                        resp.addCookie(name);
                        resp.addCookie(pwd);
                        System.out.println("成功存取cookie");
                    } else {
                        Cookie name = new Cookie("accountName", null);
                        Cookie pwd = new Cookie("accountPwd", null);
                        resp.addCookie(name);
                        resp.addCookie(pwd);
                        System.out.println("cookie已销毁");
                    }
                } else {
                    //添加登录时间失败
                    System.out.println("添加登录时间失败");
                }
                return "redirect:/index.jsp";
            //if(thisAccount.getStatus()==1 && thisAccount.getRoleStatus()==1)
            }else{
                System.out.println("该账号或角色被禁用");
                session.setAttribute("message", "该账号或角色被禁用，如有疑问，请您移步微信运营群：98438确认~");
            }
        //if (thisAccount != null) {
        } else {
            session.setAttribute("message", "用户名或密码错误啦~重新试试吧~");
            System.out.println("用户校验失败");
        }
        return "redirect:/login.jsp";
    }
    @RequestMapping("/returnLogin")
    public String returnLogin(HttpSession session){
        session.setAttribute("thisAccount",null);  //清空登录数据
        session.setAttribute("RiOneList", null);  //后台一级菜单权限
        session.setAttribute("RecOneList", null); //前台一级菜单权限
        session.setAttribute("RightDoList", null);  //操作权限
        return "redirect:/login.jsp";
    }
}